Imports System.Text

''' -----------------------------------------------------------------------------
''' Project	 : BizBlox
''' Class	 : BizBlox.UnicodeTranslator
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Provides capability of unicode character searching using latin characters
''' </summary>
''' <remarks>
''' <![CDATA[
''' The following symbols are supported
''' {Ae}
''' {Th}
''' {th}
''' {ss}
''' {ae}
''' {!}
''' {cent}
''' {pound}
''' {currency}
''' {yen}
''' {|}
''' {section}
''' {umlaut}
''' {C}
''' {^a}
''' {<<}
''' {not}
''' {-}
''' {R}
''' {_}
''' {degrees}
''' {+/-}
''' {^2}
''' {^3}
''' {'}
''' {micro}
''' {paragraph}
''' {*}
''' {cedilla}
''' {^1}
''' {^o}
''' {>>}
''' {1/4}
''' {1/2}
''' {3/4}
''' {?}
''' {*}
''' {/}
''' ]]>
''' </remarks>
''' <history>
''' 	[cinj]	16/06/2006	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class UnicodeTranslator

    Private Shared charEquivalent As String() = {"A", "A", "A", "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "N", "O", "O", "O", "O", "O", "O", "U", "U", "U", "U", "Y", "a", "a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i", "n", "o", "o", "o", "o", "o", "o", "u", "u", "u", "u", "y", "y"}
    Private Shared charUnicodeEntity As Integer() = {&HC0, &HC1, &HC2, &HC3, &HC4, &HC5, &HC7, &HC8, &HC9, &HCA, &HCB, &HCC, &HCD, &HCE, &HCF, &HD1, &HD2, &HD3, &HD4, &HD5, &HD6, &HD8, &HD9, &HDA, &HDB, &HDC, &HDD, &HE0, &HE1, &HE2, &HE3, &HE4, &HE5, &HE7, &HE8, &HE9, &HEA, &HEB, &HEC, &HED, &HEE, &HEF, &HF1, &HF2, &HF3, &HF4, &HF5, &HF6, &HF8, &HF9, &HFA, &HFB, &HFC, &HFD, &HFF}
    Private Shared transEquivalent As String() = {"Ae", "Th", "th", "ss", "ae", "th", "th", "!", "cent", "pound", "currency", "yen", "|", "section", "umlaut", "C", "^a", "<<", "not", "-", "R", "_", "degrees", "+/-", "^2", "^3", "'", "micro", "paragraph", "*", "cedilla", "^1", "^o", ">>", "1/4", "1/2", "3/4", "?", "*", "/"}
    Private Shared transUnicodeEntity As Integer() = {&HC6, &HD0, &HDE, &HDF, &HE6, &HF0, &HFE, &HA1, &HA2, &HA3, &HA4, &HA5, &HA6, &HA7, &HA8, &HA9, &HAA, &HAB, &HAC, &HAD, &HAE, &HAF, &HB0, &HB1, &HB2, &HB3, &HB4, &HB5, &HB6, &HB7, &HB8, &HB9, &HBA, &HBB, &HBC, &HBD, &HBE, &HBF, &HD7, &HF7}

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Create SQL unicode search pattern from single ASCII character
    ''' </summary>
    ''' <param name="input"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	22/07/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function SqlUnicodePattern(ByVal input As String) As String
        Dim sb As New StringBuilder
        Dim ssb As StringBuilder
        Dim i, j As Integer

        For i = 0 To input.Length - 1
            j = 0
            ssb = New StringBuilder(input.Chars(i))
            For j = 0 To charEquivalent.Length - 1
                If CStr(input.Chars(i)) = charEquivalent(j) Then
                    ssb = ssb.Append(ChrW(charUnicodeEntity(j)))
                End If
            Next
            If ssb.Length > 1 Then
                sb = sb.Append("[").Append(ssb.ToString()).Append("]")
            Else
                sb = sb.Append(input.Chars(i))
            End If
        Next
        Return sb.ToString()
    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tranaslate {key} to unicode character.
    ''' </summary>
    ''' <param name="input"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' <![CDATA[
    ''' Accepts these input forms:
    ''' {Ae} , {Th} , {th} , {ss} , {ae} , {th} , {th} , {!} , {cent} , {pound} , {currency} , {yen} , {|} , {section} , {umlaut} , {C} , {^a} , {<<} , {not} , {-} , {R} , {_} , {degrees} , {+/-} , {^2} , {^3} , {'} , {micro} , {paragraph} , {*} , {cedilla} , {^1} , {^o} , {>>} , {1/4} , {1/2} , {3/4} , {?} , {*} , {/}
    ''' ]]>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	22/07/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function SqlUnicodeTranslate(ByVal input As String) As String
        Dim i As Integer
        Dim sb As New StringBuilder(input)
        sb = sb.Replace("?", "_")
        sb = sb.Replace("*", "%")
        For i = 0 To transEquivalent.Length - 1
            sb = sb.Replace("{" & transEquivalent(i) & "}", ChrW(transUnicodeEntity(i)))
        Next
        Return sb.ToString()
    End Function

End Class
